﻿using Microsoft.AspNetCore.Mvc;
using NET6Demo.WebApi.Utility.Filters;
using Zhaoxi.NET6.Interfaces;

namespace NET6Demo.WebApi.Controllers
{
    /// <summary>
    /// AOP-Filter
    /// </summary>
    [ApiController]
    //[CustomExceptionFilter] //当前控制器下的所有的方法都会生效
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class FiltersController : ControllerBase
    {
        private readonly ILogger<FiltersController> _logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public FiltersController(ILogger<FiltersController> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id:int}")]
        //[CustomAsyncResourceFilter] //基本扩展
        //[CustomCacheResourceFilter] //扩展缓存
        //[CustomAsyncCacheResourceFilter]//扩展异步版本缓存

        public IActionResult GetUser(int id)
        {

            Console.WriteLine("=============业务逻辑处理=================");
            Console.WriteLine("=============业务逻辑处理=================");
            Console.WriteLine("=============业务逻辑处理=================");
            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            ////IAsyncResourceFilter 
            ////IResourceFilter 
            return new JsonResult(data);
        }

        /// <summary>
        /// 测试actionFilter
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        //[CustomActionFilter]
        //[CustomAsyncActionFilter]
        //[CustomCacheActionFilterAttribute]
        [TypeFilter(typeof(CustomLogActionFilterAttribute))]
        //[ServiceFilter(typeof(CustomLogActionFilterAttribute))]
        //[CustomExceptionFilter]

        public IActionResult ActionFilterApi(int id)
        {
            //IActionFilter
            //IAsyncActionFilter 
            //ActionFilterAttribute
            _logger.LogInformation("FiltersController...ActionFilterApi 被执行~~~");
            Console.WriteLine("=============业务逻辑处理=================");
            Console.WriteLine("=============业务逻辑处理=================");
            Console.WriteLine("=============业务逻辑处理=================");
            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            return new JsonResult(data);
        }

        /// <summary>
        /// 异常处理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpPut]
        //[CustomExceptionFilter]
        //[CustomAsyncExceptionFilter]
        [TypeFilter(typeof(CustomAsyncExceptionFilterAttribute))]
        //[ServiceFilter(typeof(CustomAsyncExceptionFilterAttribute))] //需要在IOC容器中注册CustomAsyncExceptionFilterAttribute
        public IActionResult ExceptionFilterApi(int id)
        {
            //IExceptionFilter
            //IAsyncExceptionFilter 
            {
                int i = 0;
                int y = 23;
                int x = y / i;//尝试除以0  不允许的，必然异常
            }

            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            return new JsonResult(new ApiResult<object>()
            {
                Success = true,
                Message = "Ok",
                Data = data
            });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult ResultFilterApi(int id)
        {
            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            return new JsonResult(new ApiResult<object>()
            {
                Success = true,
                Message = "Ok",
                Data = data
            });
        }


        /// <summary>
        /// 测试ResultFilter/AlwaysRunResultFilter
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("ResultResultFilterApi")]
        [CustomResultFilter]
        [CustomAlwayRunResultFilter]
        public IActionResult ResultResultFilterApi(int id)
        {

            var data = new
            {
                Id = id,
                Name = "Richard老师",
                Age = 36,
                DateTime = DateTime.Now.ToString()
            };
            return new JsonResult(new ApiResult<object>()
            {
                Success = true,
                Message = "Ok",
                Data = data
            });
        }
    }
}
